﻿// <copyright file="Program.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Microsoft/ Barry Dorrans</author>
// <date>2008-06-20</date>
// <summary>The main entry point for the CardWriter.</summary>

namespace SharpSTS.CardWriter
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml;

    using CardControlService;
    
    /// <summary>
    /// A card writer using the CardControlService.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Starts the application.
        /// </summary>
        /// <param name="args">Command line arguements.</param>
        public static void Main(string[] args)
        {
            try
            {
                if (args.Length == 1 || args.Length == 2)
                {
                    string outputFileName = args[0].Replace(new FileInfo(args[0]).Extension, ".crd");

                    if (args.Length == 2)
                    {
                        outputFileName = args[1];
                    }

                    outputFileName = new FileInfo(outputFileName).FullName;
                    string inputFileName = new FileInfo(args[0]).FullName;

                    CreateCard(inputFileName, outputFileName);
                }
                else
                {
                    Console.WriteLine("Usage:\n    SharpSTS.CardWriter carddetails.ini [outputfilename.crd]");
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
        }

        /// <summary>
        /// Creates a managed card file.
        /// </summary>
        /// <param name="iniFileName">Name of the ini file.</param>
        /// <param name="outputFileName">Name of the output file.</param>
        private static void CreateCard(string iniFileName, string outputFileName)
        {
            Console.WriteLine("Reading card config from \n    " + iniFileName);
            IniFile cardConfiguration = new IniFile(iniFileName);
            Console.WriteLine("  Authentication Type: " + cardConfiguration["card"]["type"].Value);

            CardControlServiceClient cardControlProxy = new CardControlServiceClient();
            cardControlProxy.Open();            

            GetRequestBody getRequestBody = new GetRequestBody
                                                {
                                                    CardName = cardConfiguration["details"]["name"].Value,
                                                    AuthenticationType =
                                                        ((AuthenticationType)Enum.Parse(
                                                            typeof(AuthenticationType),
                                                            cardConfiguration["card"]["type"].Value))
                                                };
            AuthenticationHint authenticationHint = new AuthenticationHint();

            if (getRequestBody.AuthenticationType == AuthenticationType.UserNamePassword ||
                getRequestBody.AuthenticationType == AuthenticationType.SelfIssued)
            {
                authenticationHint.Hint = cardConfiguration["Credentials"]["value"].Value;
            }

            if (!string.IsNullOrEmpty(cardConfiguration["details"]["image"].Value))
            {
                getRequestBody.ImageFileName = cardConfiguration["details"]["image"].Value;
            }

            if (!string.IsNullOrEmpty(cardConfiguration["Credentials"]["Hint"].Value))
            {
                authenticationHint.Description = cardConfiguration["Credentials"]["Hint"].Value;
            }

            getRequestBody.AuthenticationHint = authenticationHint;

            getRequestBody.UserIdentifier = cardConfiguration["Credentials"]["UserIdentifier"].Value;

            GetResponseBody response = cardControlProxy.Get(getRequestBody);
            cardControlProxy.Close();

            Console.WriteLine("Writing card config from \n    " + outputFileName);
            XmlTextWriter fileWriter = new XmlTextWriter(outputFileName, Encoding.UTF8);
            XmlElement e = response.Card;
            e.WriteTo(fileWriter);
            fileWriter.Flush();
            fileWriter.Close();
        }
    }
}
